Skip to content

feat(std): add bounds-tracked string builder#10

Open
NKS01X wants to merge 1 commit into
microsoft:publicfrom
NKS01X:feat/string-builder
Open

feat(std): add bounds-tracked string builder#10
NKS01X wants to merge 1 commit into
microsoft:publicfrom
NKS01X:feat/string-builder

Conversation

@NKS01X
Copy link
Copy Markdown

@NKS01X NKS01X commented May 3, 2026

Resolves #9

The Problem

Standard C string manipulation functions (strcat, sprintf, etc.) are notoriously prone to buffer overflows and silent truncation. lib0xc needed a safe, bounds-tracked mechanism for dynamically constructing strings within statically-allocated buffers.

The Solution

This PR introduces xc_str_builder_t and its associated API to the std module. It provides a lightweight, bounds-checked wrapper around a character array that safely handles appends and formatting without risking memory corruption.

Key Features Implemented:

  • xc_str_builder_init: Safely wraps a statically-allocated buffer, ensuring it is properly null-terminated from the start.
  • xc_str_builder_append: Appends strings while strictly enforcing capacity limits.
  • xc_str_builder_appendf: Safely formats data using vsnprintf. Includes specific logic to detect silent truncation and actively "undo" partial writes to prevent corrupting the buffer state.
  • xc_str_builder_append_char: Fast, O(1) safe single-character appends.
  • Buffer Management: Includes clear and truncate utilities for safe buffer reuse.

Testing

  • Created a comprehensive test suite in 0xtest/unit/test_str_builder.c.
  • Integrated directly with the internal UNIT_TEST framework (0xc/sys/check.h).
  • Verified 100% pass rate locally on Clang.
  • Tests specifically cover bounds overflow prevention and partial-write reversion during formatted truncation.

Notes for Reviewers

All formatting strictly adheres to the project's C guidelines, including Allman-style bracing, padded function parameters, and the required Microsoft copyright headers.

@NKS01X
Copy link
Copy Markdown
Author

NKS01X commented May 3, 2026

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: Bounds-tracked String Builder API (xc_str_builder_t)

1 participant